package example;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * 用户数据报协议 UDP
 */
public class UdpServer {
    public static void main(String s[]) {
        try {
            DatagramSocket socket = new DatagramSocket(1818);
            byte bytes[] = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            socket.receive(packet);
            String string = new String(bytes, 0, packet.getLength());
            System.out.println("接收数据: " + string);

            InetAddress address = packet.getAddress();
            int port = packet.getPort();
            String string2 = "receive";
            byte bytes2[] = string2.getBytes();
            DatagramPacket resultPacket = new DatagramPacket(bytes2, bytes2.length, address, port);
            socket.send(resultPacket);

            socket.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}





